﻿namespace SuperMarieApi.App.Controllers.Framework.DataAccess
{
    using System.Data;
    using System.Data.Common;

    using SuperMarieApi.App.Controllers.Framework.DataAccess.Config;

    public class DataCommand : DataCommandBase
    {
        private DataCommandConfig _dataCommandConfig;
        internal DataCommand(string providerName, string connectionString, DataCommandConfig dataCommandConfig)
            :base(providerName,connectionString)
        {
            this._dataCommandConfig = dataCommandConfig;
            this._dbCommand = this.CreateDbCommand();
        }

        protected override DbCommand CreateDbCommand()
        {
            this._dbCommand =base.CreateDbCommand();

            this._dbCommand.CommandText = this._dataCommandConfig.CommandText;
            CommandType commandType = CommandType.Text;
            if (this._dataCommandConfig.CommandType != 0)
            {
                commandType = this._dataCommandConfig.CommandType;
            }
            this._dbCommand.CommandType = commandType;
            this._dbCommand.CommandTimeout = this._dataCommandConfig.Timeout;

            if (this._dataCommandConfig.Parameters != null)
            {
                string pName = string.Empty;
                foreach (var p in this._dataCommandConfig.Parameters)
                {
                    this.AddParameter(p.Name, p.DbType, p.Size, p.Direction);
                }
            }

            return this._dbCommand;
        }

        public DataCommandConfig DataCommandConfig
        {
            get
            {
                return this._dataCommandConfig;
            }
        }
    }
}
